var express = require("express");
var router = express.Router();

const goodsApi = require("../api/goods");
/* GET home page. */
router.put("/:_id", async function (req, res, next) {
  const { _id } = req.params;
  const goods = req.body;
  const data = await goodsApi.update({ _id }, goods);
  res.send(data);
});
//更新状态
router.put('/update/:_id', async function (req, res, next) {
  const { _id } = req.params;
  console.log('===========>>>',_id)
  const { status } = req.body;
  console.log('<==============>',status)
  const data = await goodsApi.changeStatus({ _id, status });
  res.send(data);
});

router.get("/userId=:userId", async function (req, res, next) {
  const { userId } = req.params;
  const data = await goodsApi.getFromUserId(userId);
  res.send(data);
});
// 我发布的商品详情
router.get("/getInfo/userId=:userId", async function (req, res, next) {
  const { userId } = req.params;
  const data = await goodsApi.getFromUserIdNow(userId);
  console.log(data);
  res.send(data);
});


router.get("/", async function (req, res, next) {
  const { page, limit, query } = req.query;
  const data = await goodsApi.get({
    page,
    limit,
    query: query ? JSON.parse(query) : {},
  });
  res.send(data);
});

router.get("/query", async function (req, res, next) {
  const { page, limit, query } = req.query;
  const data = await goodsApi.queryGet({
    page,
    limit,
    query: JSON.parse(query),
  });
  res.send(data);
});
//
router.get("/get", async function (req, res, next) {
  console.log(req.query, ":::::");
  const data = await goodsApi.getName(req.query);
  res.send(data)
})

router.get("/:_id", async function (req, res, next) {
  const _id = req.params;
  const data = await goodsApi.getOne(_id);
  res.send(data);
});

router.patch("/:_id", async function (req, res, next) {
  const _id = req.params;
  const { status } = req.body;
  const data = await goodsApi.changeStatus({ _id, status });
  res.send(data);
});

router.post("/", async function (req, res, next) {
  const goods = req.body;
  const data = await goodsApi.add(goods);
  res.send(data);
});

router.delete("/:_id", async function (req, res, next) {
  const _id = req.params;
  const data = await goodsApi.del(_id);
  res.send(data);
});

module.exports = router;
